<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title>jstime</title>
        <script language="javascript" type="text/javascript">
        // syntax: return a value, this value is stored in a variable called t
        // 1000 milliseconds in one second
            function timedMsg() {
                var t=setTimeout( "alert('5 seconds times up!')",5000 );
            }
        // initialize
            var c=0;
            var t;
            var timer_is_on=0;
        // infinite loop
            function timedCount() {
                document.getElementById( 'txt' ).value=c;
                c=c+1;
                t=setTimeout( "timedCount()",1000);
            }

            function doTimer() {
                if ( !timer_is_on ) {
                    timer_is_on = 1;
                    timedCount();
                }
            }
        // stop function
            function stopCount() {
                clearTimeout( t );
                timer_is_on=0;
            }
        // number clock
            function numClock() {
                var today=new Date();
                var h=today.getHours();
                var m=today.getMinutes();
                var s=today.getSeconds();
                // add a zero
                m=checkTime(m);
                s=checkTime(s);
                document.getElementById( 'timer' ).innerHTML=h+":"+m+":"+s;
                t=setTimeout( 'numClock()',500 );
            }
        // check if the number<10
            function checkTime(num) {
                if(num<10){
                    num="0"+num;
                }
                return num;
            }
        </script>
    </head>
    <body>
        <form action="">
        	<input type="button" value="Display timed alert box" onClick="timedMsg()"/>
        </form>
        <br />
        <form action="">
            <input type="button" value="Start count" onClick="doTimer()"/>
            <input id="txt" name="" type="text" />
            <input type="button" value="Stop count" onClick="stopCount()" />
        </form>

        <hr />
        
        <form action="">
            <input type="button" value="Number Clock" onClick="numClock()" />
            <div id="timer"></div>
        </form>
        
    </body>
</html>
